<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="方案名称">
        <el-input v-model="queryParams.name" placeholder="请输入方案名称"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增按钮 -->
    <el-button type="primary" size="small" @click="handleAdd" style="margin-bottom: 15px;">+ 新增</el-button>

    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="方案名称" width="200"></el-table-column>
      <el-table-column prop="monitorfrequency" label="监测频次" width="180"></el-table-column>
      <el-table-column prop="monitorpoint" label="监测点位" width="180"></el-table-column>
      <el-table-column prop="monitorindex" label="监测指标" width="180"></el-table-column>
      <el-table-column prop="samplemode" label="采样方式" width="180"></el-table-column>
      <el-table-column prop="preservationmethod" label="保存方式" width="180"></el-table-column>
      <el-table-column prop="preproccessmethod" label="样品预处理方法"></el-table-column>
      <el-table-column prop="analyticalmethod" label="样品分析方法" width="180"></el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 20px;"
    ></el-pagination>
    <!-- 新增对话框 -->
    <el-dialog :title="form.id?'修改监测方案':'添加监测方案'" :visible.sync="dialogVisible" width="800px">
      <el-form :model="form" label-width="120px" :rules="rules" ref="form">
        <!-- 方案名称 -->
        <el-form-item label="方案名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入方案名称"></el-input>
        </el-form-item>

        <!-- 监测频次 -->
        <el-form-item label="监测频次" prop="monitorfrequency">
          <el-input v-model="form.monitorfrequency" placeholder="请输入监测频次"></el-input>
        </el-form-item>

        <!-- 监测点位 -->
        <el-form-item label="监测点位" prop="monitorpoint">
          <el-input v-model="form.monitorpoint" placeholder="请输入监测点位"></el-input>
        </el-form-item>

        <!-- 监测指标 -->
        <el-form-item label="监测指标" prop="monitorindex">
          <el-input v-model="form.monitorindex" placeholder="请输入监测指标"></el-input>
        </el-form-item>

        <!-- 采样方式 -->
        <el-form-item label="采样方式" prop="samplemode">
          <el-input v-model="form.samplemode" placeholder="请输入采样方式"></el-input>
        </el-form-item>

        <!-- 保存方式 -->
        <el-form-item label="保存方式" prop="preservationmethod">
          <el-input v-model="form.preservationmethod" placeholder="请输入保存方式"></el-input>
        </el-form-item>

        <!-- 样品预处理方法 -->
        <el-form-item label="样品预处理方法" prop="preproccessmethod">
          <el-input v-model="form.preproccessmethod" placeholder="请输入样品预处理方法"></el-input>
        </el-form-item>

        <!-- 样品分析方法 -->
        <el-form-item label="样品分析方法" prop="analyticalmethod">
          <el-input v-model="form.analyticalmethod" placeholder="请输入样品分析方法"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getMonitoringSchemeList,addMonitoringScheme,ediMonitoringscheme,removeMonitoringscheme} from './api.js'
export default {
  data() {
    return {
      queryParams: {
        name: ''
      },
      currentPage: 1,
      pageSize: 10,
      total: 1, // 总数据条数
      tableData: [],
      loading: false,
      dialogVisible: false, // 控制弹窗显示
      form: {
        name: '',
        monitorfrequency: '',
        monitorpoint: '',
        monitorindex: '',
        samplemode: '',
        preservationmethod: '',
        preproccessmethod: '',
        analyticalmethod: '',
      },
      rules: {
        name: [{ required: true, message: '请输入方案名称', trigger: 'blur' }],
        monitorfrequency: [{ required: true, message: '请输入监测频次', trigger: 'blur' }],
        monitorpoint: [{ required: true, message: '请输入监测点位', trigger: 'blur' }],
        monitorindex: [{ required: true, message: '请输入监测指标', trigger: 'blur' }],
        samplemode: [{ required: true, message: '请输入采样方式', trigger: 'blur' }],
        preservationmethod: [{ required: true, message: '请输入保存方式', trigger: 'blur' }],
        // preproccessmethod: [{ required: false, message: '请输入样品预处理方法', trigger: 'blur' }],
        // analyticalmethod: [{ required: false, message: '请输入样品分析方法', trigger: 'blur' }],
      }
    };
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() { 
      let params = {
        ...this.queryParams,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      this.loading = true
      let res = await getMonitoringSchemeList(params)
      if(res.code == 200) {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false;
      }
    },
    handleSearch() {
      this.getList();
      // 这里可以添加实际的搜索逻辑
    },
    resetForm() {
      this.queryParams.name = '';
    },
    handleAdd() {
      this.dialogVisible = true;
      this.form = {
        name: '',
        monitorfrequency: '',
        monitorpoint: '',
        monitorindex: '',
        samplemode: '',
        preservationmethod: '',
        preproccessmethod: '',
        analyticalmethod: '',
      };
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.form = {...row};
    },
       // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 这里可以提交数据到后端
          let apiFn = this.form.id ? ediMonitoringscheme : addMonitoringScheme;
          apiFn(this.form).then(res => {
            this.$message.success('提交成功');
            this.dialogVisible = false;
            this.getList(); // 刷新列表
          });
        } else {
          console.log('校验失败');
          return false;
        }
      });
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeMonitoringscheme(id).then(res =>{
          this.$message.success('删除成功');
          this.getList();

        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      // 这里可以添加分页大小改变的逻辑
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      // 这里可以添加当前页改变的逻辑
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>